import { addDialog,closeDialog } from "@/components/ReDialog";
import alarmList from '../component/alarm/index.vue'
import alarminfo from '../component/alarm/alarm-info.vue'
import individualityAlarmList from '../component/alarm/individuality-index.vue'
import lnspectionTasks from '../component/lnspection-tasks/index.vue'
import workManagement from '../component/work-management/index.vue'
import superviseAlarm from '../component/supervise-alarm/index.vue'
import superviseFamilyInfo from '../component/supervise-family/info.vue'
import individualityAlarmInfo from '../component/alarm/individuality-alarm-info.vue'
import editWorkOrderForm from '../component/work-management/form.vue'
import workoderIfos from "../component/work-management/workorder-info.vue";
import { getInfo as getAlarmInfo } from '@/api/screen/alarm/index'
import { getInfo as getFamilyInfo } from '@/api/screen/family/index'
import { familyfamily } from "@/api/screen/screenmune"
import alarmHand from '@/views/large-data-screen/component/alarmHand.vue'
import { message } from "@/utils/message";
import { getAlarmHandle } from "@/api/screen";
import { getInspectionDetailFacilities } from "@/api/screen/work-management/index";
import { usePageVariablesStoreHook } from "@/store/modules/pageVariables";

export const showAlarmInfo = (row) => {
    let formRef = ref()
    addDialog({
        title: `告警详情`,
        width: "800px",
        draggable: true,
        fullscreen: false,
        fullscreenIcon: true,
        props: {
            id: row.id
        },
        hideFooter: true,
        closeOnClickModal: false,
        contentRenderer: () => h(alarminfo, { ref: formRef }),
        openAutoFocus({ options, index }) {
            options.props = {
                isLoading: true,
                id: row.id,
                formInline: {}
            };
            getAlarmInfo({ id: row.id }).then(res => {
                if (res.code == 200) {
                    options.props.formInline = res.result;
                    // options.props.id = res.result.deviceId;
                }
                options.props.isLoading = false;
            });
        }
    });
}
export const showAlarmList = ({ title = '告警管理', defaultSearch = null } = { title: '告警管理', defaultSearch: null }) => {
    addDialog({
        title: title,
        width: "1300px",
        draggable: true,
        hideFooter: true,
        fullscreen: false,
        fullscreenIcon: true,
        closeOnClickModal: false,
        props: {
            defaultSearch: defaultSearch
        },
        contentRenderer: () => h(alarmList),


    });
    // showSuperviseAlarm()
    // showSuperviseFamilyInfo(1)
}
export const alarmHandling = (row) => {
    const formRef = ref()
    addDialog({
        title: `告警处理`,
        width: "400px",
        draggable: true,
        fullscreen: false,
        fullscreenIcon: true,
        props: {
            id: row.id,
            projectId: row.projectId,
        },
        closeOnClickModal: false,
        contentRenderer: () => h(alarmHand, { ref: formRef }),
        openAutoFocus({ options, index }) {
            options.props = {
                isLoading: false,
                id: row.id,
                projectId: row.projectId,
                formInlines: {
                    id: row.id,
                    confirm: null,
                    dealStatus: null,
                    dealView: null,
                    sellMethod: null,
                    processId: null,
                    handleUid: null,
                    noticeType: []
                }
            };
        },
        beforeSure: (done, { options }) => {
            const FormRefsa = formRef.value.getRef();
            const formData = options.props.formInlines;
            function chores() {
                message(`操作成功`, {
                    type: "success"
                });
                done();
            }
            console.log("formData", formData);

            FormRefsa.validate(valid => {
                if (valid) {
                    getAlarmHandle(formData).then(res => {
                        if (res.code == 200) {
                            chores()
                        }
                    }).catch(err => {
                        if (err !== "请求中") {
                            options.props.isLoading = false;
                        }
                    });
                }
            });
        }
    });
}

export const showIndividualityAlarmList = ({ title = '个体告警管理', defaultSearch = null } = { title: '告警管理', defaultSearch: null }) => {
    addDialog({
        title: title,
        width: "1300px",
        draggable: true,
        hideFooter: true,
        fullscreen: false,
        fullscreenIcon: true,
        closeOnClickModal: false,
        props: {
            defaultSearch: defaultSearch
        },
        contentRenderer: () => h(individualityAlarmList),
    });
}
// 处理
export const showIndividualityAlarmInfo = (row) => {
    addDialog({
        title: `个体告警`,
        hideFooter: true,
        width: "800px",
        draggable: true,
        fullscreen: false,
        fullscreenIcon: true,
        closeOnClickModal: false,
        contentRenderer: () => h(individualityAlarmInfo),
        openAutoFocus({ options, index }) {
            options.props = {
                isLoading: true,
                formInline: {}
            }
            getAlarmInfo({ id: row.id }).then(res => {
                if (res.code == 200) {
                    options.props.formInline = res.result
                }
            }).finally(() => {
                options.props.isLoading = false
            })
        },
    });
}
export const showLnspectionTasks = ({ title = '巡检任务', defaultSearch = null } = { title: '巡检任务', defaultSearch: null }) => {
    addDialog({
        title: title,
        width: "1300px",
        draggable: true,
        hideFooter: true,
        fullscreen: false,
        fullscreenIcon: true,
        closeOnClickModal: false,
        props: {
            defaultSearch: defaultSearch
        },
        contentRenderer: () => h(lnspectionTasks),
    });
}

export const workoderIfo  = (row) => {
    let formRefs = ref()
    addDialog({
        title: `工单详情`,
        width: "800px",
        draggable: true,
        fullscreen: false,
        fullscreenIcon: true,
        hideFooter: true,
        closeOnClickModal: false,
        contentRenderer: () => h(workoderIfos, { ref: formRefs }),
        openAutoFocus({ options, index }) {
          formRefs.value.closeDialog((command = "chexiao") => {
            closeDialog(options, index, { command: command });
            if(command == 'chexiao'){
                workOrderHandl('工单编辑',row)
            }
          });
          options.props = {
            isLoading: true,
            id: row.id,
          };
          getInspectionDetailFacilities({ id: row.id })
            .then(res => {
              if (res.code == 200) {
                options.props.formInline = res.result;
                options.props.isLoading = false;
              }
            })
            .finally(() => {
              options.props.isLoading = false;
            });
        }
      });
}
export const showWorkOrder = ({ title = '工单管理', defaultSearch = null } = { title: '工单管理', defaultSearch: null }) => {
    console.log('defaultSearch', defaultSearch);
    addDialog({
        title: title,
        width: "1300px",
        draggable: true,
        hideFooter: true,
        fullscreen: false,
        fullscreenIcon: true,
        closeOnClickModal: false,
        props: {
            defaultSearch: defaultSearch
        },
        contentRenderer: () => h(workManagement),
    });
}
const workOrderHandl = (title, row?: any) => {
    let formRef = ref()
    addDialog({
      title: `${title}`,
      width: "800px",
      draggable: true,
      props: {
        isLoading: null,
        formInline: {},
        edittype: title
      },
      fullscreen: false,
      fullscreenIcon: true,
      closeOnClickModal: false,
      hideFooter: true,
      contentRenderer: () => h(editWorkOrderForm, { ref: formRef }),
      openAutoFocus({ options, index }) {
        formRef.value.closeDialog((command = "sure") => {
          closeDialog(options, index, { command: command });
        });
        if (row) {
          options.props = {
            isLoading: true,
            formInline:{},
            edittype: title
          };
          getInspectionDetailFacilities({ id: row.id })
            .then(res => {
              if (res.code == 200) {
                options.props.formInline = res.result.workerOrderInfo;
              }
            })
            .finally(() => {
              options.props.isLoading = false;
            });
        } else {
          options.props = {
            isLoading: false,
            formInline: {},
            edittype: title
          };
          options.props.formInline.projectId = usePageVariablesStoreHook().getScreen()?.project?.id
        }
      }
    });
  };
export const showSuperviseAlarm = ({ title = '监管告警', defaultSearch = null } = { title: '监管告警', defaultSearch: null }) => {
    addDialog({
        title: title,
        width: "1300px",
        draggable: true,
        hideFooter: true,
        fullscreen: false,
        fullscreenIcon: true,
        closeOnClickModal: false,
        props: {
            defaultSearch: defaultSearch
        },
        contentRenderer: () => h(superviseAlarm),
    });
}

export const showSuperviseFamilyInfo = (familyId, type = 2) => {
    addDialog({
        title: '家庭信息',
        width: "800px",
        draggable: true,
        hideFooter: true,
        fullscreen: false,
        fullscreenIcon: true,
        closeOnClickModal: false,
        contentRenderer: () => h(superviseFamilyInfo),
        open({ options, index }) {
            options.props = {
                isLoading: true,
                formInline: {}
            }
            if (type == 2) {
                getFamilyInfo({ familyId: familyId }).then(res => {
                    if (res.code == 200) {
                        options.props.formInline = res.result
                    }
                }).finally(() => {
                    options.props.isLoading = false
                })
            } else {
                familyfamily({ familyId: familyId }).then(res => {
                    if (res.code == 200) {
                        options.props.formInline = res.result
                    }
                }).finally(() => {
                    options.props.isLoading = false
                })
            }

        },
    });
}
